package com.mixide.ui.part.register;

import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.mixal.Mix;

public class RegisterTableViewer {
	
	private Table table;
	private TableViewer tableViewer;
	private final Mix mix;
	private final ITableLabelProvider labelProvider;

	// Set the table column property names
	private final String NO_COLUMN = "no";
	private final String SIGN_COLUMN = "sign";
	private final String BYTE1_COLUMN = "byte1";
	private final String BYTE2_COLUMN = "byte2";
	private final String BYTE3_COLUMN = "byte3";
	private final String BYTE4_COLUMN = "byte4";
	private final String BYTE5_COLUMN = "byte5";
	private final String TO_CHAR_COLUMN = "toChar";
	private final String TO_INT_COLUMN = "toInt";

	// Set column names
	private String[] columnNames = new String[] { 
			NO_COLUMN, 
			SIGN_COLUMN,
			BYTE1_COLUMN,
			BYTE2_COLUMN,
			BYTE3_COLUMN,
			BYTE4_COLUMN,
			BYTE5_COLUMN,
			TO_CHAR_COLUMN,
			TO_INT_COLUMN
	};

	public RegisterTableViewer(Composite parent, Mix mix, ITableLabelProvider labelProvider) {
		this.mix = mix;
		this.labelProvider = labelProvider;
		this.addChildControls(parent);
	}

	public void dispose() {
		tableViewer.getLabelProvider().dispose();
	}

	private void addChildControls(Composite composite) {
		createTable(composite);

		createTableViewer();
		
		tableViewer.setContentProvider(new RegisterContentProvider());
		tableViewer.setLabelProvider(this.labelProvider);
		tableViewer.setCellModifier(new RegisterCellModifier(tableViewer));
		tableViewer.setInput(this.mix);
	}

	private void createTable(Composite parent) {
		int style = SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | 
					SWT.FULL_SELECTION | SWT.HIDE_SELECTION;

		table = new Table(parent, style);
		
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
					
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		for (int i = 0; i < this.columnNames.length; i++) {
			TableColumn column = new TableColumn(table, SWT.CENTER, i);	
			column.setText(this.columnNames[i]);
			column.setWidth(50);
			if (i == 0) {
				column.setWidth(50);
			} else if (i == 1) {
				column.setWidth(40);
			} else if (i == 7) {
				column.setWidth(60);
			} else if (i == 8) {
				column.setWidth(80);
			}
		}
	}

	private void createTableViewer() {

		tableViewer = new TableViewer(table);
		tableViewer.setUseHashlookup(true);
		
		tableViewer.setColumnProperties(columnNames);

		CellEditor[] editors = new CellEditor[columnNames.length];
		for (int i = 1; i < this.columnNames.length; i++) {
			editors[i] = new TextCellEditor(table);
		}
		editors[1] = new ComboBoxCellEditor(table, new String[] { "+", "-" }, SWT.READ_ONLY);
		tableViewer.setCellEditors(editors);
	}

	class RegisterContentProvider implements IStructuredContentProvider {
		
		public Object[] getElements(Object parent) {
			if (!(parent instanceof Mix)) {
				return null;
			}
			Mix mix = (Mix) parent;
			RegisterRow[] rows = new RegisterRow[9];
			rows[0] = new RegisterRow("rA", mix.rA());
			rows[1] = new RegisterRow("rA", mix.rA());
			rows[2] = new RegisterRow("rA", mix.rA());
			for (int i = 1; i <= 6; i++) {
				rows[i + 2] = new RegisterRow("rI" + i, mix.rI(i));
			}
			return rows;
		}
	}

	public List<String> getColumnNames() {
		return Arrays.asList(this.columnNames);
	}
}